<template>
  <el-card shadow="hover">
    <div class="components-container">
      <aside style="text-align: center">
        请上传你的图片作为您的头像,
        下面是上传后展示的头像图片
      </aside>

      <pan-thumb :image="image" />

      <el-button type="primary" icon="el-icon-upload" style="position: absolute;bottom: 15px;margin-left: 130px;" @click="uploadhandle">
        更换头像{{ avatar_sha }} {{ avatar_fileName }}
      </el-button>

      <image-cropper
        v-show="imagecropperShow"
        :key="imagecropperKey"
        :width="300"
        :height="300"
        :params="requestParams"
        field="file"
        url="/files/uploadFile"
        @close="close"
        @crop-upload-success="cropSuccess"
      />
    </div>
  </el-card>
</template>

<script>
import { mapGetters } from 'vuex'
import ImageCropper from '@/components/ImageCropper'
import PanThumb from '@/components/PanThumb'
import { getUserCode } from '@/utils/auth'
export default {
  name: 'AvatarUpload',
  components: { ImageCropper, PanThumb },
  data() {
    return {
      requestParams: {}, // 文件上传携带参数
      dialogAvatarUploadVisible: false, // 是否展示上传的头像对话框
      imagecropperShow: false, // 点击展示上传头像的对话框
      imagecropperKey: 0,
      image: this.avatar
    }
  },
  computed: {
    ...mapGetters([
      'avatar_sha',
      'avatar',
      'avatar_fileName'
    ])
  },
  methods: {
    cropSuccess(resData) {
      this.imagecropperShow = false
      this.imagecropperKey = this.imagecropperKey + 1
      this.image = resData.main.access_url
      this.uploadUserAvatarinfo(resData.main.access_url, resData.main.sha, resData.main.file_name)
    },
    close() {
      this.imagecropperShow = false
    },
    uploadhandle() {
      const sha = this.avatar_sha
      const filename = this.avatar_fileName
      console.log(this.avatar_fileName)
      Object.assign(this.requestParams,
        {
          userCode: getUserCode(),
          sha: sha,
          avatarFileName: filename
        }
      )
      this.imagecropperShow = true
    },
    // 数据保存到数据库
    async uploadUserAvatarinfo(url, sha, filename) {
      // 实例化数据对象
      const obj = {
        userCode: getUserCode(),
        avatar: url,
        avatarGiteeSha: sha,
        avatarFileName: filename
      }
      // 发送请求 以及保存到vuex存储
      const res = await this.$store.dispatch('user/setAvatar', obj)
      const header = res.data.respOutMsgHeader
      // 请求响应处理
      if (header.respCode === 200) {
        this.$message({
          type: 'success',
          iconClass: 'el-icon-loading',
          message: '用户头像上传成功，若无法查看到新头像请尝试刷新页面。',
          duration: 1.5 * 1000
        })
        // 上传成功后关闭上传对话框
        this.imagecropperShow = false
      } else {
        this.$message({
          type: 'error',
          iconClass: 'el-icon-loading',
          message: '头像信息上传失败,错误信息：' + header.msg,
          duration: 3 * 1000
        })
      }
    }
  }
}
</script>

<style scoped>
  .avatar{
    width: 200px;
    height: 200px;
    border-radius: 50%;
  }
</style>

